<?php

namespace App\Orchid\Screens\Page;

use App\Models\Page;
use Orchid\Screen\Screen;
use Orchid\Screen\Actions\Link;
use Orchid\Support\Facades\Toast;
use Orchid\Support\Facades\Layout;
use Orchid\Screen\TD;

class PageListScreen extends Screen
{
    /**
     * Fetch data to be displayed on the screen.
     *
     * @return array
     */
    public function query(): iterable
    {
        return [
            'pages' => Page::with(['user'])
                ->orderBy('created_at', 'desc')
                ->paginate(),
        ];
    }

    /**
     * The name of the screen displayed in the header.
     */
    public function name(): ?string
    {
        return '页面管理';
    }

    /**
     * Display header description.
     */
    public function description(): ?string
    {
        return '管理所有静态页面';
    }

    /**
     * The screen's action buttons.
     *
     * @return \Orchid\Screen\Action[]
     */
    public function commandBar(): iterable
    {
        return [
            Link::make('新建页面')
                ->icon('plus')
                ->route('platform.pages.create'),
        ];
    }

    /**
     * The screen's layout elements.
     *
     * @return \Orchid\Screen\Layout[]
     */
    public function layout(): iterable
    {
        return [
            Layout::table('pages', [
                TD::make('title', '页面标题')
                    ->sort()
                    ->filter(TD::FILTER_TEXT)
                    ->render(function (Page $page) {
                        return Link::make($page->title)
                            ->route('platform.pages.edit', $page);
                    }),
                    
                TD::make('slug', 'Slug')
                    ->sort()
                    ->render(fn (Page $page) => $page->slug),
                    
                TD::make('user.name', '作者')
                    ->render(fn (Page $page) => $page->user->name ?? '-'),
                
                TD::make('status', '状态')
                    ->render(function (Page $page) {
                        return match($page->status ?? 'draft') {
                            'published' => '<span class="text-success">已发布</span>',
                            'draft' => '<span class="text-warning">草稿</span>',
                            default => $page->status,
                        };
                    }),
                    
                TD::make('template', '模板')
                    ->render(function (Page $page) {
                        return $page->template ?: 'default';
                    }),
                    
                TD::make('created_at', '创建时间')
                    ->sort()
                    ->render(function (Page $page) {
                        return $page->created_at->format('Y-m-d H:i');
                    }),
                    
                TD::make('操作')
                    ->align(TD::ALIGN_CENTER)
                    ->width('150px')
                    ->render(function (Page $page) {
                        return Link::make('编辑')
                            ->icon('pencil')
                            ->route('platform.pages.edit', $page);
                    }),
            ]),
        ];
    }
    
    /**
     * Remove page
     *
     * @param Page $page
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function remove(Page $page)
    {
        $page->delete();
        
        Toast::info('页面已删除');
    }
}